class User < ActiveRecord::Base
  include SavageBeast::UserInit

  has_many :submitted_issues, :class_name => 'Issue', :foreign_key => :submitter_id
  has_many :adoptions
  has_many :adopted_issues, :through => :adoptions, :source => :issue
  has_many :subscriptions
  has_many :subscribed_issues, :through => :subscriptions, :source => :issue
  has_many :votes
  has_many :memberships
  has_many :projects, :through => :memberships

  # TODO: add migration to add this field
  def admin
    true
  end

  alias admin? admin

  def self.authenticate(name)
    user = User.find_by_dn(name)
    if user
      puts "Found user!"
      user
    else
      puts "Creating user!"
      user = User.new
      user.dn = name
      user.save!
      user
    end
  end

  # returns t/f depending on the user's vote for issue
  def voted_for?( issue )
    Vote.voted_for?( issue, self )
  end

  # returns t/f depending on the user's vote for issue
  def voted_against?( issue )
    Vote.voted_against?( issue, self )
  end

  # adds a positive vote for the issue
  def vote_for( issue )
    Vote.for( issue, self )
  end

  # adds a negative vote for the issue
  def vote_against( issue )
    Vote.against( issue, self )
  end

  # returns the total number of positive votes minus the total number of
  # negative votes for the user
  def disposition
    Vote.disposition_of( self )
  end

  # returns t/f depending on the user's ownership (assignment/adoption)
  def owns?( issue )
    Adoption.owned?( issue, self )
  end

  # creates an "adoption" relationship between the user and the issue that
  # denotes responsibility
  def adopt( issue )
    Adoption.adopt( issue, self )
  end

  # removes an "adoption" relationship
  def disown( issue )
    Adoption.disown( issue, self ) do |assigner|
      yield assigner if block_given?
    end
  end

  # returns t/f depending on the user's subscription status to issue
  def subscribed_to?( issue )
    Subscription.subscribed?( issue, self )
  end

  # subscribes to issue
  def subscribe( issue )
    Subscription.subscribe( issue, self )
  end

  # unsubscribes from issue
  def unsubscribe( issue )
    Subscription.unsubscribe( issue, self )
  end

  # creates a membership in project for the user
  def join( project )
    Membership.join( project, self )
  end

  # removes a membership in project for the user
  def leave( project )
    Membership.leave( project, self )
  end

  # returns whether the user is a member of project
  def member_of?( project )
    Membership.member?( project, self )
  end

  # returns an array of comments attached to the given model authored by the
  # user if no model is given, all user comments are returned.
  def comments( model = Comment )
    model.find_comments_by_user( self )
  end

  def username
    dn.split.last
  end

  def display
    username
  end

  def inspect
    "User( #{display} )"
  end

  ### Start methods required for savage_beast plugin

  # Required as part of savage_beast
  def display_name
    display
  end

  # Required as part of savage_beast
  # FIXME: project owners admin on the forums?
  # Blue: Sure; for our purposes anyway.  I don't expect too many trolls.
  def admin?
    true
  end

  # Required as part of savage_beast
  # FIXME: do we want presence tracking?
  # Blue: Not if it takes any work, for now.
  def currently_online
    false
  end

  ### End methods required for savage_beast plugin

end
